package com.fk.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fk.dao.impl.XinWenXiangQingDaoImpl;
import com.fk.entity.XinWenFenLei;
import com.fk.entity.XinWenXiangQing;
import com.fk.uitl.BaseServlet;
import com.fk.uitl.DBManager;
import com.fk.uitl.Result;
import com.google.gson.Gson;

/**
 * Servlet implementation class AdminNewsDetaileServlet
 */
public class AdminNewsDetaileServlet extends BaseServlet {

	
	
	
	/**
	 * 查询资讯分类内容
	 * localhost:8080/wuyijiuchaoliufushiguan/adminNewsDetaile?reqName=getZiXunLeiBie
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getZiXunLeiBie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		XinWenXiangQingDaoImpl xWen = new XinWenXiangQingDaoImpl();
		List<XinWenXiangQing> xinList = xWen.getXinWenXiangQing();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(xinList);
		out.print(res);
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 删除资讯分类内容
	 * localhost:8080/wuyijiuchaoliufushiguan/adminNewsDetaile?reqName=delZiXunLeiBie
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	
	public void delZiXunLeiBie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int xId = Integer.parseInt(request.getParameter("xId"));	// 接受前台传来的参数
			
		XinWenXiangQingDaoImpl xDao = new XinWenXiangQingDaoImpl();	// 创建实现类对象
		int x  = xDao.delectXinWenNeiRongId(xId);	// 调用删除方法
		response.setCharacterEncoding("UTF-8"); // 设置编码格式
		PrintWriter out = response.getWriter(); // out对象
		
		if (x > 0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		out.flush();
		out.close();
	}
	/**
	 * 条件查询
	 * localhost:8080/wuyijiuchaoliufushiguan/adminNewsDetaile?reqName=getChaXun
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getChaXun(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int xinWenNeiRongId=0;
		if(request.getParameter("xinWenNeiRongId")!=null && !request.getParameter("xinWenNeiRongId").equals("")) {
			xinWenNeiRongId=Integer.parseInt(request.getParameter("xinWenNeiRongId"));
		}
		String xinWenBiaoTi=request.getParameter("xinWenBiaoTi");
		String xinWenNeiRong=request.getParameter("xinWenNeiRong");
		int xinWenFenLeiId=0;
		if(request.getParameter("xinWenFenLeiId")!=null && !request.getParameter("xinWenFenLeiId").equals("")) {
			xinWenFenLeiId=Integer.parseInt(request.getParameter("xinWenFenLeiId"));
		}
		String xinWenFenLei=request.getParameter("xinWenFenLei");
		//���ݽ��ܵĲ������������������
		XinWenXiangQing q=new XinWenXiangQing(xinWenNeiRongId, xinWenBiaoTi, xinWenNeiRong, xinWenFenLeiId, xinWenFenLei);
		
		XinWenXiangQingDaoImpl ndDao=new XinWenXiangQingDaoImpl();//创建实现类对象
		List<XinWenXiangQing> ndList=ndDao.getNewsDetail(q);//��ѯ����������Ѷ���󼯺�
		
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式
		PrintWriter out=response.getWriter();//out对象
		
		Gson gson=new Gson();//Gson����
		String res=gson.toJson(ndList);//��ndListת��Ϊjson�ַ���
		
		out.print(res);//��Ӧ����
		
		out.flush();
		out.close();
			
			
		}
	/**
	 * 添加
	 * localhost:8080/wuyijiuchaoliufushiguan/adminNewsDetaile?reqName=addZiXunLeiBie
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addZiXunLeiBie(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter out=response.getWriter();
			
			XinWenXiangQingDaoImpl zxDao = new XinWenXiangQingDaoImpl();
			String xinWenNeiRong=request.getParameter("xinWenNeiRong");
			int	xinWenFenLeiId=Integer.parseInt(request.getParameter("xinWenFenLeiId"));
			
			
			System.out.println(xinWenFenLeiId);
			
			
		    String xinWenBiaoTi=request.getParameter("xinWenBiaoTi");
		    if(xinWenBiaoTi !=null && ! xinWenBiaoTi.equals("")) {
		    	List<XinWenXiangQing> xList =  zxDao.getXinWenXiangQingName(xinWenBiaoTi);
		    	if(xList.size() == 0) {
		    		XinWenXiangQing xq=new XinWenXiangQing(0, xinWenBiaoTi, xinWenNeiRong, xinWenFenLeiId);
					
					int n=zxDao.addXinWenNeiRongId(xq);
					if (n>0) {
						out.print(Result.toClient(true, "添加成功", ""));
					} else {
						out.print(Result.toClient(false, "添加失败", ""));
					}
		    	}else {
		    		out.print(Result.toClient(false, "添加新闻标题已存在", ""));
		    	}
		    }
		  
			
			
			out.flush();
			out.close();
		
	}
	
	/**
	 * 添加
	 * localhost:8080/wuyijiuchaoliufushiguan/adminNewsDetaile?reqName=getZiXunXiangQing
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getZiXunXiangQing(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter out=response.getWriter();
			XinWenXiangQingDaoImpl zxDao=new XinWenXiangQingDaoImpl();
			int id=Integer.parseInt(request.getParameter("cId"));
			XinWenXiangQing zx=zxDao.getXinWenXiangQing(id);
			Gson gson=new Gson();
			String res=gson.toJson(zx);
			out.print(res);
			 
		    
			out.flush();
			out.close();
		
	}
	/**
	 * 修改
	 * localhost:8080/wuyijiuchaoliufushiguan/adminNewsDetaile?reqName=updateZiXunXiangQing
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateZiXunXiangQing(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter out=response.getWriter();
			XinWenXiangQingDaoImpl zxDao=new XinWenXiangQingDaoImpl();
			int xinWenNeiRongId=Integer.parseInt(request.getParameter("xinWenNeiRongId"));
			 String xinWenBiaoTi=request.getParameter("xinWenBiaoTi");
			 String xinWenNeiRong=request.getParameter("xinWenNeiRong");
			 int xinWenFenLeiId=Integer.parseInt(request.getParameter("xinWenFenLeiId"));
			 XinWenXiangQing xq=new XinWenXiangQing(xinWenNeiRongId, xinWenBiaoTi, xinWenNeiRong, xinWenFenLeiId);
		    int n=zxDao.upadateXiangQing(xq);
		    
		    if (n>0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(true, "修改失败", ""));

			}
			out.flush();
			out.close();
		
	}

}
